package com.ccl.keyassistant.activity;
/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.activity
 *文件名     AddKeyActivity
 *创建者     ccl
 *创建时间   2019-03-10 19:10
 *注解       手动添加密钥
 */

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ccl.keyassistant.R;
import com.ccl.keyassistant.db.DBDataHelper;
import com.ccl.keyassistant.db.DBHelper;
import com.ccl.keyassistant.entity.KeyModel;
import com.ccl.keyassistant.utils.FileUtil;
import com.ccl.keyassistant.utils.L;
import com.ccl.keyassistant.utils.StaticClass;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AddKeyActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mPassword;
    private EditText mRemark;
    private Button mShowPass;
    private Button mAddOk;
    private Button mAddCancel;

    //当前密码是否显示
    private boolean currPassState = false;
    private EditText mAccoutOrFileName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addkey_activity);

        initView();

    }

    private void initView() {
        mAccoutOrFileName = (EditText) findViewById(R.id.addkey_fileOrAccount_name);
        mPassword = (EditText) findViewById(R.id.addkey_key);
        mRemark = (EditText) findViewById(R.id.addkey_comment);
        mShowPass = (Button) findViewById(R.id.addkey_show);
        mAddOk = (Button) findViewById(R.id.addkey_ok);
        mAddCancel = (Button) findViewById(R.id.addkey_cancel);
        //设置点击事件
        mShowPass.setOnClickListener(this);
        mAddOk.setOnClickListener(this);
        mAddCancel.setOnClickListener(this);

        //初始化ToolBar
        View tb = findViewById(R.id.addkey_bar);
        Toolbar mToolBar = (Toolbar) tb.findViewById(R.id.toolbar);
        tb.findViewById(R.id.action_bar_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //设置toolbar
        setSupportActionBar(mToolBar);
        //去掉默认显示的title
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //控制密码是否显示
            case R.id.addkey_show:
                if (!currPassState) {
                    //显示密码
                    mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    mPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                currPassState = !currPassState;
                break;
            //手动添加密钥成功
            case R.id.addkey_ok:
                //校验是否为空
                String accountOrFileName = mAccoutOrFileName.getText().toString().trim();
                String password = mPassword.getText().toString().trim();
                String remark = mRemark.getText().toString().trim();

                if (accountOrFileName.length() <= 0 || password.length() <= 0 || remark.length() <= 0) {
                    Toast.makeText(this, "输入框不可以为空", Toast.LENGTH_SHORT).show();
                } else {
                    final KeyModel keyModel = new KeyModel();
                    keyModel.file_hash = null;
                    keyModel.file_name = accountOrFileName;
                    keyModel.password = password;
                    keyModel.remark = remark;
                    keyModel.create_time = FileUtil.getTime();
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            //连接数据库，写入数据
                            DBDataHelper dbDataHelper = DBDataHelper.getInstance(getApplicationContext());
                            long insert = dbDataHelper.insert(DBHelper.TABLE_KEY, keyModel);
                            if (insert <= 0) {
                                L.w("手动添加密钥插入失败");
                            }
                        }
                    });

                    finish();
                }
                break;
            case R.id.addkey_cancel:
                finish();
                break;
            default:
                break;
        }
    }


}
